﻿using System;
using System.Linq;
using System.Threading;
using MainWorkflowTestClient.UniverSys;

namespace MainWorkflowTestClient
{
    class Program
    {
        static void Main(string[] args)
        {            
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Client Started. Waiting a few seconds for MainKernelWorkflow");
            
            var client = new MainWorkflowClient();
            client.Open();

            
            var our_session = client.AutorizateMe("apilon", ";kjasdf");
            if (our_session != null)
                Console.WriteLine("1 Step: Authorization \nguid={0} \ndebug={1}", our_session.SessionGuid, our_session.__debug_string);
            else
                Console.WriteLine("our_session is null");

            Console.WriteLine("2 Step: Get StudentsWithGroups");
            var students = client.GetStudentsWithGroups(our_session);
            if (students != null)
                foreach (var groupDto in students.Groups)
                {
                    Console.WriteLine("Group: {0}, Id={1}",
                                         groupDto.Name,
                                      groupDto.Id);

                    foreach (var studentDto in groupDto.Students)
                        Console.WriteLine("\tStudent: {0}, Id={1}",
                                          studentDto.Name,
                                          studentDto.Id);
                }
            else
                Console.WriteLine("students is null");

            Guid studentId = students.Groups.Last().Students.First().Id;
            Console.WriteLine("3. Step: GetExamsByStudent {0}", studentId);
            var exams = client.GetExamsByStudent(our_session, studentId);

            if (exams != null)
            {
                Console.WriteLine("Получили список экзаменов для студента с Id={0} Name={1}",
                                  exams.Student.Id,
                                  exams.Student.Name);

                foreach (var examDto in exams.Exams)
                {
                    Console.WriteLine("Экзамен в ауд. {0} в {1} у преподавателя {2} по предмету {3}",
                        examDto.Auditory,
                        examDto.Date,
                        examDto.Teacher.Name,
                        examDto.Subject.Name);

                    Console.WriteLine("Получена оценка: {0}", examDto.Mark);
                }
            }
            else
                Console.WriteLine("exams is null");

            Console.WriteLine("4. Step: GetTeacherWeekSchedule");

            var week = client.GetTeacherWeekSchedule(our_session, Guid.Empty, DateTime.Now);
            TestScheduleDatabaseFunctionality(week);

            Console.WriteLine("5. Step: GetTeacherDaySchedule");
            var day = client.GetTeacherDaySchedule(our_session, Guid.Empty, DateTime.Parse("13.01.2011"));
            
            Console.WriteLine("At day {0} for teacher Id={1}, Name={2}",
                day.RequestedDay, day.Teacher.Id, day.Teacher.Name);

            if (day.Lines != null)
                foreach (TeacherLineWSScheduleDto line in day.Lines)
                {
                    Console.WriteLine("-----------------------------");
                    Console.WriteLine("in {0} at {1} will be subject Id={2}, Name={3}, Hour={4}",
                        line.Auditory, line.Time.TimeOfDay,
                        line.Subject.Id, line.Subject.Name, line.Subject.Hours);

                    if (line.GroupsGuids != null)
                        foreach (Guid groupguid in line.GroupsGuids)
                            Console.WriteLine("   group={0}", groupguid);
                }

            Console.WriteLine("6. Step: SearchBook");
            BookDto trybook = new BookDto();
            trybook.Author = "Таненбаум";
            BooksBaseListDto booklist = client.SearchBook(our_session, trybook);

            if (booklist == null || booklist.Books == null || booklist.Books.Count() == 0)
                Console.WriteLine("Book not found");
            else
            foreach (BookDto book in booklist.Books)
            {
                Console.WriteLine("Id={0}, Author={1}, Title={2}, PagesCount={3}, Year={4}",
                    book.Id,
                    book.Author,
                    book.Title,
                    book.PagesCount,
                    book.Year);
            }

            Console.WriteLine("7. Step: GetListOfBooksWhitchPersonGetInLibrary");
            BooksWithPersonListDto bookWPlist = client.GetListOfBookWhichPersonGetInLibrary(our_session, day.Teacher.Id);
            if (bookWPlist != null)
            {
                Console.WriteLine("Person Id={0}, Name={1}",
                    bookWPlist.HolderPerson.Id, bookWPlist.HolderPerson.Name);

                if (bookWPlist.Books != null)
                foreach (BookDto book in bookWPlist.Books)
                {
                    Console.WriteLine("Id={0}, Author={1}, Title={2}, PagesCount={3}, Year={4}",
                        book.Id,
                        book.Author,
                        book.Title,
                        book.PagesCount,
                        book.Year);
                }
            }
            

            client.Close();
            Console.ReadKey();            
        }


        public static void TestScheduleDatabaseFunctionality(TeacherWeekFullDataScheduleDto week)
        {
            Console.WriteLine("At day {0} ({1}) for teacher with Id={2}, Name={3}",
                week.RequestedWeek.Day, week.RequestedWeek.DayOfWeek,
                week.Teacher.Id, week.Teacher.Name);

            foreach (TeacherDayItemWSScheduleDto day in week.Days)
            {
                Console.WriteLine("=================================");
                Console.WriteLine(day.WeekDay);
                if (day.Lines != null)
                    foreach (TeacherLineWSScheduleDto line in day.Lines)
                    {
                        Console.WriteLine("-----------------------------");
                        Console.WriteLine("in {0} at {1} will be subject id={2}, hours={3}, name={4}",
                            line.Auditory, line.Time.TimeOfDay,
                            line.Subject.Id, line.Subject.Hours, line.Subject.Name);

                        if (line.GroupsGuids != null)
                            foreach (Guid groupguid in line.GroupsGuids)
                                Console.WriteLine("   group={0}", groupguid);
                    }
            }

        }
    }
}
